<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 *  This file is part of CI-ActiveRecord
 *
 *  CI-ActiveRecord is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  CI-ActiveRecord is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with CI-ActiveRecord.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * ActiveRecord_Behaviour_Timestampable
 */
class ActiveRecord_Behaviour_Timestampable extends ActiveRecord_Behaviour {
    
    /**
     * Array of timestampable options
     */
    protected $_options = array('created' =>  array('name'          => 'created_at',
                                                    'type'          => 'date',
                                                    'format'        => 'Y-m-d H:i:s',
                                                    'disabled'      => false),
                                'updated' =>  array('name'          => 'updated_at',
                                                    'type'          => 'date',
                                                    'format'        => 'Y-m-d H:i:s',
                                                    'disabled'      => false,
                                                    'onSave'        => true));
    
    /**
     * __construct
     * 
     * @param object $invoker Instance of ActiveRecord
     * @param array $options array of timestampable options 
     */
    public function __construct(ActiveRecord $invoker, array $options=array())
    {
        parent::__construct($invoker, $options);
        $this->getInvoker()->addListener(new ActiveRecord_Listener_Timestampable($this->_options));
    }
}

/**
 * ActiveRecord_Listener_Timestampable
 */
class ActiveRecord_Listener_Timestampable extends ActiveRecord_Listener {
    
    /**
     * Array of timestampable options
     */
    protected $_options = array();
    
    /**
     * __construct
     *
     * @param string $options 
     * @return void
     */
    public function __construct(array $options)
    {
        $this->_options = $options;
    }
        
    /**
     * preSave
     */
    public function preSave(ActiveRecord_Event $event)
    {
        if ( ! $this->_options['created']['disabled']) {
            $createName = $this->_options['created']['name'];
            $event->getInvoker()->$createName = $this->getTimestamp('created');
        }
        
        if ( ! $this->_options['updated']['disabled'] AND $this->_options['updated']['onSave']) {
            $updatedName = $this->_options['updated']['name'];
            $event->getInvoker()->$updatedName = $this->getTimestamp('updated');
        }
    }
    
    /**
     * preUpdate
     */
    public function preUpdate(ActiveRecord_Event $event)
    {
        if ( ! $this->_options['updated']['disabled']) {
            $updatedName = $this->_options['updated']['name'];
            $event->getInvoker()->$updatedName = getTimestamp('updated');
        }
    }
    
    /**
     * getTimestamp
     *
     * Gets the timestamp in the correct format
     *
     * @param string $type 
     * @return string
     */
    public function getTimestamp($type)
    {
        $options = $this->_options[$type];

        if ($options['type'] == 'date') {
            return date($options['format'], time());
        } else {
            return time();
        }
    }
}